/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package servlet;

import java.io.*;
import java.net.*;
import model.*;
import dbcontroller.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author micheal
 */
public class RegisterServlet extends HttpServlet {
   
    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException { 
        
        
        
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String id=request.getParameter("UserName");
        //System.out.print("$$$$$$$$$$$$###############################");
        //System.out.print("$$$$$$$$$$$$$$$$$"+id+"########################################");
        String password=request.getParameter("PassWord");
        String name=request.getParameter("RealName");
        String email=request.getParameter("Email");
        System.out.println(email);
        String question=request.getParameter("Question");
        String answer=request.getParameter("Answer");
        //String address=request.getParameter("address");
        String verification=request.getParameter("WordVerification");
        String address="";
        String detail="";        
        String rand=(String)request.getSession().getAttribute("rand");
        //System.out.print(verification+"fdsafdsafsa");
        
        if(!verification.equals(rand)){
        
            ErrorReport(8,request,response);
            return;
        }
        
        if(id.length()>16){
            ErrorReport(1,request,response);  
            return;
        }
        if(password.length()>14){        
            ErrorReport(2,request,response);
            return;
        }
        if(name.length()>12){        
            ErrorReport(3,request,response);
            return;
        }
        //if(email.length()>14){
        //    ErrorReport(4,request,response);  
        //    return;
        //}
        if(question.length()>20){        
            ErrorReport(5,request,response);
            return;
        }
        if(answer.length()>20){        
            ErrorReport(6,request,response);
            return;
        }
        if(address.length()>40){        
            ErrorReport(7,request,response);
            return;
        }
        
        
        User register=User.getInstance();
        register.SetID(id);
        register.SetPassword(password);
        register.SetName(name);
        register.SetEmail(email);
        register.SetQualifiedQuestion(question);
        register.SetQualifiedAnswer(answer);
        register.SetAddress(address);
        register.SetDetail(detail);
        
        UserController regist=UserController.getInstance();
        
        if(regist.CheckUser(register)){           
             
            RequestDispatcher dispatcher = request.getRequestDispatcher("");                                
            dispatcher.forward(request, response);
        
        }else{            
            regist.AddUser(register);
            RequestDispatcher dispatcher = request.getRequestDispatcher("Succeed.jsp");                                
            dispatcher.forward(request, response);
        
        }
        
        
        
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet 方法。单击左侧的 + 号以编缉代码。">
    /** 
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
           
            processRequest(request, response);       
    }
    
    public void ErrorReport(int type, HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
    
        RequestDispatcher dispatcher = request.getRequestDispatcher("register.jsp");           
        dispatcher.forward(request, response);
        
    }

    /** 
    * Returns a short description of the servlet.
    */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
